﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace nr.Tombola.Windows.Player
{
    public partial class ManualCardCreateForm : Form
    {
        public Cards.Card Card
        {
            get
            {
                Cards.Card result = new Cards.Card();
                result.Name = txtName.Text;
                for (int row = 0; row < 3; ++row)
                    for (int dec = 0; dec < 9; ++dec)
                    {
                        TextBox t = GetNumber(row, dec);
                        if (t.Text.Length > 0)
                            result.Place(int.Parse(t.Text), row);
                    }
                return result;
            }
            set
            {
                txtName.Text = value.Name;
                for (int row = 0; row < 3; ++row)
                    for (int dec = 0; dec < 9; ++dec)
                    {
                        TextBox t = GetNumber(row, dec);
                        int number = value[row][dec];
                        if (number == 0)
                            t.Text = string.Empty;
                        else
                            t.Text = number.ToString();
                    }
            }
        }

        private TextBox GetNumber(int row, int dec)
        {
            return (TextBox)cardLayout.Controls.Find(string.Format("txt{0}_{1}", row, dec), false)[0];
        }

        public ManualCardCreateForm()
        {
            InitializeComponent();
        }

        private void AcceptDigit(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar));
        }

        private void ValidateNumbers(object sender, CancelEventArgs e)
        {
            TextBox t = sender as TextBox;
            if (t.Text.Length > 0)
            {
                int number = int.Parse(t.Text);
                e.Cancel = (number < 1) || (number > 90);
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!IsValid())
                MessageBox.Show(this, "Controllare con attenzione i dati inseriti!", "[nr.Tombola] Controller di Gioco", MessageBoxButtons.OK);
            else
                DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private bool IsValid()
        {
            if (txtName.Text.Length == 0)
                return false;

            bool countValid = true;
            bool rangeValid = true;
            for (int row = 0; row < 3 && countValid && rangeValid; ++row)
            {
                int count = 0;
                for (int dec = 0; dec < 9 && rangeValid; ++dec)
                {
                    int decStart = dec * 10;
                    if (dec == 0) decStart = 1;
                    int decEnd = dec * 10 + 9;
                    if (dec == 8) decEnd = 90;
                    TextBox t = GetNumber(row, dec);
                    if (t.Text.Length > 0)
                    {
                        int number = int.Parse(t.Text);
                        rangeValid = number >= decStart && number <= decEnd;
                        ++count;
                    }
                }
                countValid = count == 5;
            }

            return (countValid && rangeValid);
        }
    }
}